<?php

namespace JLPay\Services\Trans\B2c;

use JLPay\Services\BaseService;
use JLPay\Models\Trans\B2c\MicroPayRequest;
use JLPay\Models\Trans\B2c\MicroPayResponse;
use JLPay\Models\Trans\B2c\MicroPayPreAuthRequest;
use JLPay\Models\Trans\B2c\MicroPayPreAuthResponse;

/**
 * 微支付服务
 */
class MicroPayService extends BaseService
{
    /**
     * 获取API URL
     *
     * @param string $requestClass 请求类名
     * @param string|null $apiPath 接口路径
     * @return string 完整的API URL
     */
    public function getApiUrl(string $requestClass, ?string $apiPath = null): string
    {
        $baseUrl = $this->config->getUrl();
        
        if ($apiPath) {
            return $baseUrl . $apiPath;
        }
        
        // 动态获取请求类中定义的API_URL常量
        $apiUrl = constant($requestClass . '::API_URL');
        
        if (!$apiUrl) {
            // 如果没有定义API_URL，使用默认路径
            return $baseUrl . "/open/trans";
        }
        
        // 直接返回完整路径
        return $baseUrl . $apiUrl;
    }

    /**
     * 微支付
     *
     * @param MicroPayRequest $request 微支付请求
     * @return MicroPayResponse 微支付响应结果
     */
    public function microPay(MicroPayRequest $request): MicroPayResponse
    {
        return $this->post($request, MicroPayResponse::class);
    }

    /**
     * 微支付预授权
     *
     * @param MicroPayPreAuthRequest $request 微支付预授权请求
     * @return MicroPayPreAuthResponse 微支付预授权响应结果
     */
    public function microPayPreAuth(MicroPayPreAuthRequest $request): MicroPayPreAuthResponse
    {
        return $this->post($request, MicroPayPreAuthResponse::class);
    }
} 